#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年9月22日

@author: chenyongbing
'''
import sys,os
import logging
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

'''
    碧桂园 历史数据轨迹
'''

sys.path.append(os.path.join(current_dir,'../'))
from base.retail_datas import myRetailDatas
from base.zhimind_datas import myZhimind
from base.time_method import myTimeMethod
# from base.hiveServer2 import HiveClient2
rolePath = '/Udisk/new_role_rerun'

startTime = '2015-09-10'
endTime = '2015-09-21'
comp_id = 227

# myHiveClient2 = HiveClient2(host='zm03')
# hiveTab='hivehistoryrole_rerun'
comp_group = int(myRetailDatas.get_comp_group(comp_id=comp_id))

days = myTimeMethod.get_slist_between_st_et(st=startTime, et=endTime)

shop_groups = myRetailDatas.get_groups_by_comp( comp_id=comp_id)

logging.basicConfig(level = logging.INFO)
datas = {}
shop_groups_str =[]
for sg in shop_groups:
    shop_groups_str.append(str(sg))

for day in days:
    rolefile  = '%s/%s/part-00000'%(rolePath,day)
    if not os.path.exists(rolefile):
        logging.error('Role File %s Not Exists.'%rolefile)
        sys.exit(1)
    #2015-09-14,32011678,b8:5a:73:ca:d1:c2,2015-09-14 17:32:34,2015-09-14 17:33:47,73000,1,Samsung
    logging.info('Start %s'%day)
    
    detail_datas = []
    
#     query = 'select day,grp,mac,st,et,dur,role,fac,mobile from %s where day="%s" and role=3 and grp in (%s)'%(hiveTab,day,','.join(shop_groups_str))
#     rets = myHiveClient2.SelectAll(query)
#     i = 0
#     for lineList in rets:
#         i+=1
#         if i == 1:
#             print lineList
#         line = ''
    for line in open(rolefile).read().split('\n'):
        lineList = line.split(',')
        if len(lineList)<8:
            logging.warn('Role Msg %s Warn.'%line)
            continue
        shop_group = lineList[1]
        if int(shop_group) not in shop_groups:continue
         
        role = lineList[6]
        if int(role) != 3:continue
        mac = lineList[2]
        st = lineList[3]
        et = lineList[4]
        dur = int(lineList[5])/1000
         
        if len(lineList)==9:
            mobile = lineList[8]
        else:
            mobile = None
        #print day,shop_group,mac,st,et,role,dur,mobile
        
        
        
        if mac not in datas.keys():
            datas[mac] = {'comp_group':comp_group,
                          'mobile':mobile,
                          'mobile_addr':None,
                          'first_group':shop_group,
                          'first_time':st,
                          'last_time':st,
                          'visit_days':[day]
                          }
        else:
            if datas[mac]['last_time']< st:
                datas[mac]['last_time'] = st
            if datas[mac]['first_time'] > st:
                datas[mac]['first_time'] = st
                datas[mac]['first_group'] = shop_group
            if day not in datas[mac]['visit_days']:
                datas[mac]['visit_days'].append(day)
        detail_datas.append([comp_group,mac,day,st,shop_group,dur])
    query = 'insert into customer_access_traces (comp_group,mac,day,access_time,access_group,duration) VALUES (%s,%s,%s,%s,%s,%s)'    
    print "%s  : %s"%(query,len(detail_datas))
    logging.info("%s  : %s"%(query,len(detail_datas)))
    myZhimind.executemany(query,detail_datas)


stats_datas = []
for mac , values in datas.items():
    #print mac, values
    comp_group = values['comp_group']
    #mac = values['mac']
    mobile = values['mobile']
    mobile_addr = values['mobile_addr']
    first_group = values['first_group']
    first_time = values['first_time']
    last_time = values['last_time']
    visit_count = len(values['visit_days'])
    
    
    stats_datas.append([comp_group,mac,mobile,first_group,first_time,last_time,visit_count])
        
query = 'insert into customer_access_stats (comp_group,mac,mobile,first_group,first_time,last_time,visit_count) VALUES (%s,%s,%s,%s,%s,%s,%s)'
#     query = 'insert into customer_access_stats set comp_group=%s, mobile=%s , first_group=%s , first_time=%s,last_time=%s,visit_count=%s \
#                     ON DUPLICATE KEY UPDATE last_time=%s,visit_count=visit_count+%s'
#     
logging.info("%s : %s"%(query,len(stats_datas)))
print "%s : %s"%(query,len(stats_datas))
myZhimind.executemany(query,stats_datas)




